package com.mlingdu.supercloudreader.ui.gank.child;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import com.mlingdu.supercloudreader.R;
import com.mlingdu.supercloudreader.adapter.EverydayAdapter;
import com.mlingdu.supercloudreader.app.Constants;
import com.mlingdu.supercloudreader.base.BaseFragment;
import com.mlingdu.supercloudreader.bean.AndroidBean;
import com.mlingdu.supercloudreader.bean.FrontpageBean;
import com.mlingdu.supercloudreader.databinding.FooterItemEverydayBinding;
import com.mlingdu.supercloudreader.databinding.FragmentEverydayBinding;
import com.mlingdu.supercloudreader.databinding.HeaderItemEverydayBinding;
import com.mlingdu.supercloudreader.http.ACache;
import com.mlingdu.supercloudreader.http.RequestImpl;
import com.mlingdu.supercloudreader.model.EverydayModel;
import com.mlingdu.supercloudreader.utils.DebugUtil;
import com.mlingdu.supercloudreader.utils.GlideImageLoader;
import com.mlingdu.supercloudreader.utils.SPUtils;
import com.mlingdu.supercloudreader.utils.TimeUtil;
import com.youth.banner.listener.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

import rx.Subscription;

/**
 * 项目名：SuperCloudReader
 * 包名：com.mlingdu.supercloudreader.ui.gank.child
 * Created by daimaren on 2017/6/30.
 */

public class EverydayFragmentEx extends BaseFragment<FragmentEverydayBinding>{
	private static final  String TAG = "EverydayFragmentEx";
	private ACache maCache;
	private ArrayList<List<AndroidBean>> mLists;
	private ArrayList<String> mBannerImages;
	private EverydayModel mEverydayModel;
	private HeaderItemEverydayBinding mHeaderBinding;
	private FooterItemEverydayBinding mFooterBinding;

	private View mHeaderView;
	private View mFooterView;
	private EverydayAdapter mEverydayAdapter;
	private boolean mIsPrepared = false;
	private boolean mIsFirst = true;
	// 是否是上一天的请求
	private boolean isOldDayRequest;
	private RotateAnimation animation;
	// 记录请求的日期
	String year = getTodayTime().get(0);
	String month = getTodayTime().get(1);
	String day = getTodayTime().get(2);
	@Override
	public int setContent() {
		return R.layout.fragment_everyday;
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		showContentView();
		bindingView.llLoading.setVisibility(View.VISIBLE);
		animation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		animation.setDuration(3000);//设置动画持续时间
		animation.setInterpolator(new LinearInterpolator());//不停顿
		animation.setRepeatCount(10);
		bindingView.ivLoading.setAnimation(animation);
		animation.startNow();

		maCache = ACache.get(getContext());
		mEverydayModel = new EverydayModel();
		mBannerImages = (ArrayList<String>) maCache.getAsObject(Constants.BANNER_PIC);
		DebugUtil.error("----mBannerImages: " + (mBannerImages == null));
		DebugUtil.error("----mLists: " + (mLists == null));

		mHeaderBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.header_item_everyday, null, false);
		// 设置本地数据点击事件等
		initLocalSetting();
		initRecyclerView();

		mIsPrepared = true;
		loadData();
	}
	/**
	 * 获取当天日期
	 */
	private ArrayList<String> getTodayTime() {
		String data = TimeUtil.getData();
		String[] split = data.split("-");
		String year = split[0];
		String month = split[1];
		String day = split[2];
		ArrayList<String> list = new ArrayList<>();
		list.add(year);
		list.add(month);
		list.add(day);
		return list;
	}
	@Override
	protected void loadData() {
		// 显示时轮播图滚动
		if (mHeaderBinding != null && mHeaderBinding.banner != null) {
			mHeaderBinding.banner.startAutoPlay();
			mHeaderBinding.banner.setDelayTime(4000);
		}

		if (!mIsVisible || !mIsPrepared) {
			return;
		}

		String oneData = SPUtils.getString("everyday_data", "2016-11-26");
		if (!oneData.equals(TimeUtil.getData())) {// 是第二天
			if (TimeUtil.isRightTime()) {//大于12：30,请求

				isOldDayRequest = false;
				mEverydayModel.setData(getTodayTime().get(0), getTodayTime().get(1), getTodayTime().get(2));
				showRotaLoading(true);
				loadBannerPicture();
				showContentData();
			} else {// 小于，取缓存没有请求前一天

			}
		} else {// 当天，取缓存没有请求当天

		}
	}
	private void initRecyclerView() {
		bindingView.xrvEveryday.setPullRefreshEnabled(false);
		bindingView.xrvEveryday.setLoadingMoreEnabled(false);
		if (mHeaderView == null) {
			mHeaderView = mHeaderBinding.getRoot();
			bindingView.xrvEveryday.addHeaderView(mHeaderView);
		}
		if (mFooterView == null) {
			mFooterBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.footer_item_everyday, null, false);
			mFooterView = mFooterBinding.getRoot();
			bindingView.xrvEveryday.addFootView(mFooterView, true);
			bindingView.xrvEveryday.noMoreLoading();
		}
		bindingView.xrvEveryday.setLayoutManager(new LinearLayoutManager(getContext()));
		// 需加，不然滑动不流畅
		bindingView.xrvEveryday.setNestedScrollingEnabled(false);
		bindingView.xrvEveryday.setHasFixedSize(false);
		bindingView.xrvEveryday.setItemAnimator(new DefaultItemAnimator());
	}
	private void initLocalSetting() {
		mEverydayModel.setData(getTodayTime().get(0), getTodayTime().get(1), getTodayTime().get(2));
//        DebugUtil.error("" + year + month + day);
		// 显示日期,去掉第一位的"0"
	}
	private void showRotaLoading(boolean isLoading) {
		if (isLoading) {
			bindingView.llLoading.setVisibility(View.VISIBLE);
			bindingView.xrvEveryday.setVisibility(View.GONE);
			animation.startNow();
		} else {
			bindingView.llLoading.setVisibility(View.GONE);
			bindingView.xrvEveryday.setVisibility(View.VISIBLE);
			animation.cancel();
		}
	}
	private void loadBannerPicture() {
		mEverydayModel.showBanncerPage(new RequestImpl() {
			@Override
			public void loadSuccess(Object object) {
				if (mBannerImages == null) {
					mBannerImages = new ArrayList<String>();
				} else {
					mBannerImages.clear();
				}
				FrontpageBean bean = (FrontpageBean) object;
				if (bean != null && bean.getResult() != null && bean.getResult().getFocus() != null && bean.getResult().getFocus().getResult() != null) {
					final List<FrontpageBean.ResultBeanXXXXXXXXXXXXXX.FocusBean.ResultBeanX> result = bean.getResult().getFocus().getResult();
					if (result != null && result.size() > 0) {
						for (int i = 0; i < result.size(); i++) {
							//获取所有图片
							mBannerImages.add(result.get(i).getRandpic());
						}
						mHeaderBinding.banner.setImages(mBannerImages).setImageLoader(new GlideImageLoader()).start();
						mHeaderBinding.banner.setOnBannerClickListener(new OnBannerClickListener() {
							@Override
							public void OnBannerClick(int position) {
								position = position - 1;
								// 链接没有做缓存，如果轮播图使用的缓存则点击图片无效
								if (result.get(position) != null && result.get(position).getCode() != null
										&& result.get(position).getCode().startsWith("http")) {
									//WebViewActivity.loadUrl(getContext(), result.get(position).getCode(), "加载中...");
								}
							}
						});
						maCache.remove(Constants.BANNER_PIC);
						maCache.put(Constants.BANNER_PIC, mBannerImages, 30000);
					}
				}
			}

			@Override
			public void loadFailed() {

			}

			@Override
			public void addSubscription(Subscription subscription) {
				EverydayFragmentEx.this.addSubscription(subscription);
			}
		});
	}
	/**
	 * 加载正文内容
	 */
	private void showContentData() {
		mEverydayModel.showRecyclerViewData(new RequestImpl() {
			@Override
			public void loadSuccess(Object object) {
				if (mLists != null) {
					mLists.clear();
				}
				mLists = (ArrayList<List<AndroidBean>>) object;
				if (mLists.size() > 0 && mLists.get(0).size() > 0) {
					setAdapter(mLists);
				} else {
					//requestBeforeData();
				}
			}

			@Override
			public void loadFailed() {
				if (mLists != null && mLists.size() > 0) {
					return;
				}
				showError();
			}

			@Override
			public void addSubscription(Subscription subscription) {
				EverydayFragmentEx.this.addSubscription(subscription);
			}
		});
	}
	private void setAdapter(ArrayList<List<AndroidBean>> lists) {
		showRotaLoading(false);
		if (mEverydayAdapter == null) {
			mEverydayAdapter = new EverydayAdapter();
		} else {
			mEverydayAdapter.clear();
		}
		mEverydayAdapter.addAll(lists);
//        DebugUtil.error("----111111 ");
//        bindingView.xrvEveryday.setAdapter(mEverydayAdapter);
//        mEverydayAdapter.notifyDataSetChanged();
//        DebugUtil.error("----222222 ");
		maCache.remove(Constants.EVERYDAY_CONTENT);
		// 缓存三天，这样就可以取到缓存了！
		maCache.put(Constants.EVERYDAY_CONTENT, lists, 259200);

		if (isOldDayRequest) {
			ArrayList<String> lastTime = TimeUtil.getLastTime(getTodayTime().get(0), getTodayTime().get(1), getTodayTime().get(2));
			SPUtils.putString("everyday_data", lastTime.get(0) + "-" + lastTime.get(1) + "-" + lastTime.get(2));
		} else {
			// 保存请求的日期
			SPUtils.putString("everyday_data", TimeUtil.getData());
		}
		mIsFirst = false;

		bindingView.xrvEveryday.setAdapter(mEverydayAdapter);
		mEverydayAdapter.notifyDataSetChanged();
	}
	/**
	 * 取缓存
	 */
	private void getACacheData() {
		if (!mIsFirst) {
			return;
		}

		if (mBannerImages != null && mBannerImages.size() > 0) {
			mHeaderBinding.banner.setImages(mBannerImages).setImageLoader(new GlideImageLoader()).start();
		} else {
			loadBannerPicture();
		}
		mLists = (ArrayList<List<AndroidBean>>) maCache.getAsObject(Constants.EVERYDAY_CONTENT);
		if (mLists != null && mLists.size() > 0) {
			setAdapter(mLists);
		} else {
			showRotaLoading(true);
			showContentData();
		}
	}
	/**
	 * 没请求到数据就取缓存，没缓存一直请求前一天数据
	 */
	private void requestBeforeData() {
		mLists = (ArrayList<List<AndroidBean>>) maCache.getAsObject(Constants.EVERYDAY_CONTENT);
		if (mLists != null && mLists.size() > 0) {
			setAdapter(mLists);
		} else {
			// 一直请求，知道请求到数据为止
			ArrayList<String> lastTime = TimeUtil.getLastTime(year, month, day);
			mEverydayModel.setData(lastTime.get(0), lastTime.get(1), lastTime.get(2));
			year = lastTime.get(0);
			month = lastTime.get(1);
			day = lastTime.get(2);
			showContentData();
		}
	}

	/**
	 * 无数据返回时，暂时去掉
	 */
	private void setEmptyAdapter() {
		showRotaLoading(false);
		// 保存请求的日期
		SPUtils.putString("everyday_data", TimeUtil.getData());

		mIsFirst = false;
	}
	@Override
	protected void onInvisible() {
		// 不可见时轮播图停止滚动
		if (mHeaderBinding != null && mHeaderBinding.banner != null) {
			mHeaderBinding.banner.stopAutoPlay();
		}
	}
}
